package com.flycent.hqhs.ui;

import java.lang.ref.WeakReference;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.flycent.hqhs.App;
import com.flycent.hqhs.R;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.http.WebException;
import com.flycent.hqhs.ui.view.SimpleTitleBar;

/**
 *  修改密码
 * @author fbl
 */
public class ModifyPwdActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.old_pwd_editText) EditText mOldPwdEdit;
	@ViewInject(id = R.id.new_pwd_editText) EditText mNewPwdEdit;
	@ViewInject(id = R.id.confirm_pwd_editText) EditText mConfirmPwdEdit;
	@ViewInject(id = R.id.next_step_btn, click = "butClick") Button mSubmitBut;
	
	private Handler mHandler = new MyHandler(this);

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_modify_password);
		FinalActivity.initInjectedView(this); //放于布局之后，否则会出现空指针

	}

	@Override
	protected void onTitleBarCreated(SimpleTitleBar titleBar) {
		super.onTitleBarCreated(titleBar);
		titleBar.setTitle(R.string.title_modify_login_pwd);
	}

	public void butClick(View v){
		if( v == mSubmitBut ){
			//判空
			if( invalidateText(mOldPwdEdit, R.string.info_old_pwd_donot_empty) 
					|| invalidateText(mNewPwdEdit, R.string.info_new_pwd_donot_empty)
					|| invalidateText(mConfirmPwdEdit, R.string.info_confirm_pwd_donot_empty) ) {
				return;
			}
			//判断两次输入的新密码是一样
			if( !getText(mNewPwdEdit).equals( getText(mConfirmPwdEdit)) ) {
				App.showShortToast( getString(R.string.info_new_confirm_pwd_donot_match) );
				return;
			}
			//判断新旧密码是否一样
			if( getText(mOldPwdEdit).equals( getText(mNewPwdEdit)) ){
				App.showShortToast( getString(R.string.info_new_ped_donot_match_old));
				return;
			}

			doModifyPwdRequest(getText(mOldPwdEdit), getText(mNewPwdEdit));
		}
	}

	private void doModifyPwdRequest(final String oldPwd,final String newPwd) {
		new Thread(){
			public void run() {
				try {
					WebClient.getInstance(ModifyPwdActivity.this).passwordUpdate(oldPwd, newPwd);
					mHandler.sendEmptyMessage(0);
				} catch (WebException e) {
					e.printStackTrace();
				}
			};
		}.start();

	}
	
	@SuppressLint("HandlerLeak") 
	private class MyHandler extends Handler{
		
		private WeakReference<Activity> mActivity;
		
		private MyHandler(Activity activity){
			mActivity = new WeakReference<Activity>(activity);
		}
		
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if( mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					App.showShortToast("密码修改成功");
					ModifyPwdActivity.this.finish();
					break;

				default:
					break;
				}
			}
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacksAndMessages(null);
	}

}
